//
//  LYFielService.m
//  LYGame
//
//  Created by 花花 on 2017/7/24.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "LYFielService.h"
#import <SDWebImage/SDImageCache.h>
@implementation LYFielService

//计算单个文件大小
+(float)fileSizeAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:path]){
        long long size=[fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size/1024.0/1024.0;
    }
    return 0;
}

//清理缓存文件
+(void)clearCache:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            //如有需要，加入条件，过滤掉不想删除的文件
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
//     [[SDImageCache sharedImageCache] clearDisk]; //关键是这句 4.0之后没有该方法 则直接使用下面的方法即可
    [[SDImageCache sharedImageCache]clearDiskOnCompletion:nil];
}
//计算目录大小
+(double)folderSizeAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    double folderSize;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            folderSize +=[LYFielService fileSizeAtPath:absolutePath];
        }
        //SDWebImage框架自身计算缓存的实现
    folderSize+=[[SDImageCache sharedImageCache] getSize]/1024.0/1024.0;
    return folderSize;
    }
    return 0;
}
//缓存路径
+(NSString *)getPath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
    return path;
}
@end
